/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.messages;

import cz.muni.fi.diplomka.servicelayer.dto.Message;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MessageManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.PlayerManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MessageManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.PlayerManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.EmptyPanel;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.FeedbackPanel;

/**
 *
 * @author Jakub Kutil
 */
public class MessagePage extends WebPage {

    private MessageManager messageManager = new MessageManagerImpl();
    private PlayerManager playerManager = new PlayerManagerImpl();

    public MessagePage(PageParameters parameters) {
        final Long id;
        final Long messageId;
        final String type;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));
            add(new MessagePanel("messagePanel", p));
            if (parameters.containsKey("messageId") && parameters.containsKey("type")) {

                messageId = Long.parseLong(parameters.getString("messageId"));
                type = parameters.getString("type");

                Message message = messageManager.findMessage(messageId);
                if (message.isUnread()) {
                    message.setUnread(false);
                    messageManager.editMessage(message);
                }
                add(new Label("from", playerManager.findPlayerById(message.getIdFrom()).getNickName()));
                add(new Label("timestamp", message.getTime().toString()));
                add(new Label("subject", message.getSubject()));
                add(new Label("body", message.getBody()));

                if (message.getIdTo().equals(id)) {
                    p.add("messageId", messageId.toString());
                    p.add("type", type);
                    add(new DeletePanel("deletePanel", p));
                } else {
                    add(new EmptyPanel("deletePanel"));
                }
            }
        }
    }
}
